package dfl.bysj.service.impl;

import dfl.bysj.config.RedisKey;
import dfl.bysj.entity.Academy;
import dfl.bysj.entity.ReserveTime;
import dfl.bysj.mapper.ReserveTimeMapper;
import dfl.bysj.service.ReserveTimeService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicReference;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author DFL
 * @since 2022-04-26
 */
@Service
public class ReserveTimeServiceImpl extends ServiceImpl<ReserveTimeMapper, ReserveTime> implements ReserveTimeService {

    private RedisTemplate redisTemplate;

    @Autowired
    public void setRedisTemplate(RedisTemplate redisTemplate) {
        this.redisTemplate = redisTemplate;
    }

    private List<ReserveTime> reserveTimes;


    private AtomicReference<ReserveTime> reserveTimeAtomicReference= new AtomicReference<>(new ReserveTime());

    private void getReserveTimes(){
        reserveTimes= (List<ReserveTime>) redisTemplate.opsForValue().get(RedisKey.ALL_RESERVE_TIME);
        if (reserveTimes==null){
            updateRedisOfReserveTimes();
        }
    }

    private void updateRedisOfReserveTimes(){
        reserveTimes=list();
        redisTemplate.opsForValue().set(RedisKey.ALL_RESERVE_TIME,reserveTimes,7, TimeUnit.DAYS);
    }

    @Override
    public List<ReserveTime> getAllReserveTimes() {
        getReserveTimes();
        return reserveTimes;
    }
}
